package com.smart.test.proxy.advice;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther a123
 * @create 2018-08-15 15:19
 * @desc 动态切面
 */
public class GreetingDynamicPointcut extends DynamicMethodMatcherPointcut {

    private static List<String> specialClientList = new ArrayList<String>();

    static {
        specialClientList.add("John");
        specialClientList.add("Tom");
    }

    public ClassFilter getClassFilter(){
        return new ClassFilter() {
            public boolean matches(Class<?> aClass) {
                System.out.println("调用getClassFileter()对" + aClass.getName() + "做静态检测");
                return Waiter.class.isAssignableFrom(aClass);
            }
        };
    }

    public boolean matches(Method method, Class clazz){
        System.out.println("调用matchs(method, clazz)" + clazz.getName() + "." + method.getName() + "做静态检测");
        return "greetTO".equals(method.getName());
    }


    public boolean matches(Method method, Class<?> aClass, Object[] objects) {
        System.out.println("调用mathcs(method, clazz)" + aClass.getName() + "." + method.getName() + "做动态检测");
        String clientName = (String)objects[0];
        return specialClientList.contains(clientName);
    }
}
